package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.MsChgPlanBean;
import com.koron.css2.meterService.bean.MsChgPlanDetailBean;
import com.koron.css2.meterService.dto.MsChgPlanDetailDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.*;

/**
 * 换表添加（简易版无审批流程，直接登记就换表）
 * @author admin
 * @since 2021.10.15
 * @version 2.1
 *
 */
public class MsChgAdd implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Map> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "添加成功", Map.class);
		try {
			MsChgPlanBean msChgPlan = JsonUtils.objectToPojo(req.getData(), MsChgPlanBean.class);
			MsChgPlanMapper mapper = factory.getMapper(MsChgPlanMapper.class);
			MsChgPlanDetailMapper msChgPlanDetailMapper = factory.getMapper(MsChgPlanDetailMapper.class);
			UserInfoMapper userInfoMapper=factory.getMapper(UserInfoMapper.class);
			List<MsChgPlanDetailBean> msChgPlanDetails = msChgPlan.getMsChgPlanDetails();
			if(msChgPlanDetails == null || msChgPlanDetails.size() == 0)
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "换表明细不能为空", Map.class);
			msChgPlan.setMeterAmount(msChgPlanDetails.size());
			msChgPlan.setBillNo(Tools.getBillNo(factory, "MS_CHG_PLAN")); //生成单据
			msChgPlan.setBillDate(new Date());
			msChgPlan.setBackfillAmount(0);
			msChgPlan.setReceiptType(1);
			msChgPlan.setCreateInfo(userInfo);
			mapper.addMsChgPlan(msChgPlan);
			List<String> userNos=new ArrayList<>();
			List<String> stopUserNos=new ArrayList<>();
			for(MsChgPlanDetailBean msChgPlanDetail : msChgPlanDetails) {  //添加换表明细
				MsChgPlanDetailDto oldMeter = msChgPlanDetailMapper.selectByOldMeterNo(msChgPlanDetail.getOldMeterNo());
				if(oldMeter != null) {
					info.setCode(Constant.MESSAGE_INT_FAIL);
					info.setDescription("水表编号" + msChgPlanDetail.getOldMeterNo()+",用户编号"+oldMeter.getUserNo() + "已存在！");
					factory.close(false);
					return info;
				}
				UserBean user=userInfoMapper.getUserInfoByUserNo(msChgPlanDetail.getUserNo(),UserBean.USER);
				if("0".equals(user.getStatus())){
					userNos.add(user.getUserNo());
				}
				if("2".equals(user.getStatus())){
					stopUserNos.add(user.getUserNo());
				}
				if(msChgPlanDetail.getMargin() == null)
					msChgPlanDetail.setMargin(0);
				msChgPlanDetail.setCreateName(userInfo.getUserInfo().getName());
				msChgPlanDetail.setId(Tools.getObjectId());
				msChgPlanDetail.setReceiptId(msChgPlan.getId());
				msChgPlanDetailMapper.addMsChgPlanDetail(msChgPlanDetail);
			}
			if(userNos.size()!=0){
//				return MessageBean.create(Constant.ILLEGAL_PARAMETER, userInfo.toString()+"用户已销户，无法做换表操作", null);
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription(userNos.toString()+"用户已销户，无法做换表操作");
				factory.close(false);
				return info;
			}
			if(stopUserNos.size()!=0){
				info.setCode(Constant.MESSAGE_INT_FAIL);
				info.setDescription(stopUserNos.toString()+"用户已停用，无法做换表操作");
				factory.close(false);
				return info;
			}
			Map<String, String> data = new HashMap<String, String>();
			data.put("id", msChgPlan.getId());
			info.setData(data);
			BaseReceiptAccessoryMapper accessoryMapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
			if(!StringUtils.isBlank(msChgPlan.getTempId()))		//修改附件ID
				accessoryMapper.updateAccessoryReceiptId(msChgPlan.getId(), msChgPlan.getTempId());
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("添加失败");
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
